/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 
package de.rockon.fuzzy.controller.gui.propertyeditor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import de.rockon.fuzzy.controller.gui.tree.treehelper.TreeClickEvent;
import de.rockon.fuzzy.controller.model.FuzzyController;
import de.rockon.fuzzy.controller.model.FuzzyPoint;
import de.rockon.fuzzy.controller.model.FuzzySet;
import de.rockon.fuzzy.controller.model.FuzzyVariable;
import de.rockon.fuzzy.controller.model.commands.ActionCommandPool;
import de.rockon.fuzzy.controller.model.listener.IModelObserver;
import de.rockon.fuzzy.controller.model.listener.IModelSubject;

/**
 * Property Panel
 */
public class PropertyPanel extends JPanel implements IModelObserver, ActionListener {
	private static final long serialVersionUID = -8041286992288771910L;
	private PropertyController propertyController;
	private PropertyVariable propertyVariable;
	private PropertySet propertySet;
	private PropertyPoint propertyPoint;
	private PropertyToolBar propertyToolBar;
	private JPanel panelPlaceHolder;

	private PropertyBase panel;

	public PropertyPanel(IModelSubject subject) {
		subject.registerObserver(this);
		setLayout(new BorderLayout(0, 0));
		setPreferredSize(new Dimension(200, 200));
		propertyToolBar = new PropertyToolBar(subject, this);
		propertyController = new PropertyController();
		propertyVariable = new PropertyVariable();
		propertySet = new PropertySet();
		propertyPoint = new PropertyPoint();
		panelPlaceHolder = new JPanel(new BorderLayout(0, 0));

		FormLayout layout = new FormLayout("1px, left:pref, 1px", "1px, pref, 1px");

		PanelBuilder builder = new PanelBuilder(layout);
		CellConstraints cc = new CellConstraints();

		this.add(new JLabel("Property Editors"));
		builder.add(panelPlaceHolder, cc.xy(2, 2));
		// builder.add(propertyToolBar, cc.xy(2, 4));
		add(builder.getPanel());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		final String cmd = e.getActionCommand();

		if (cmd.equals(ActionCommandPool.PROPERTY_ACCEPT)) {
			panel.saveProperties();
		} else if (cmd.equals(ActionCommandPool.PROPERTY_RESET)) {
			panel.resetProperties();
		}
	}

	@Override
	public void addEvent(IModelSubject subject, Object addItem) {
		// not needed
	}

	@Override
	public void changeEvent(IModelSubject subject, Object changeItem) {
		if (changeItem instanceof TreeClickEvent) {
			panelPlaceHolder.removeAll();

			if (subject instanceof FuzzyController) {
				panel = propertyController;
				panel.getPropertyValues((FuzzyController) subject);
			} else if (subject instanceof FuzzyVariable) {
				panel = propertyVariable;
				panel.getPropertyValues((FuzzyVariable) subject);
			} else if (subject instanceof FuzzySet) {
				panel = propertySet;
				panel.getPropertyValues((FuzzySet) subject);
			} else if (subject instanceof FuzzyPoint) {
				panel = propertyPoint;
				panel.getPropertyValues((FuzzyPoint) subject);
			}
			panelPlaceHolder.add(panel);
			panelPlaceHolder.updateUI();
		}
	}

	@Override
	public void removeEvent(IModelSubject subject, Object remItem) {
		// not needed
	}

}
